package com.jk.mongodb.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jk.entity.DummyOrder;
import com.jk.entity.Order;
import com.jk.entity.Shop;
import com.jk.mongodb.MongodbDao;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author 周慧茹
 * 创建时间 2018/4/16  18:37
 * 邮箱 960297232@qq.com
 */
@Repository
public class MongodbDaoImpl implements MongodbDao {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public DummyOrder querycookie(String cookiewy) {
        Query query = new Query();

        if(cookiewy!=null){
            Criteria regex = Criteria.where("cookieid").regex(cookiewy);
            query.addCriteria(regex);
        }

        DummyOrder order = mongoTemplate.findOne(query, DummyOrder.class, "t_dummyOrder");

        return order;
    }

    @Override
    public void removeCookie(String cookiewy) {
        Criteria  criteria =  Criteria.where("cookieid").is(cookiewy);

        Query query = new Query(criteria);

        mongoTemplate.remove(query,"t_dummyOrder");


    }

    @Override
    public Order queryshopCarbuuserid(String userid) {

        Query query = new Query();
        if(userid!=null){
            Criteria regex = Criteria.where("userid").is(userid);
            query.addCriteria(regex);
        }

        Order order = mongoTemplate.findOne(query,Order.class, "t_shop");

        return order;
    }

    @Override
    public void updateMap(String id, HashMap<String, HashMap<String, List<Shop>>> maps) {

        Criteria criteria =Criteria.where("id").is(id);
        Query query = new Query(criteria);

        Update update =Update.update("maps",maps);

        mongoTemplate.updateFirst(query,update,Order.class,"t_shop");


    }

    @Override
    public DummyOrder queryDummyShoppingCar(String cookiewy) {

        Query query = new Query();
        if(cookiewy!=null){
            Criteria regex = Criteria.where("cookieid").is(cookiewy);
            query.addCriteria(regex);
        }

        DummyOrder dummyOrder = mongoTemplate.findOne(query,DummyOrder.class, "t_dummyOrder");

        return dummyOrder;
    }

    @Override
    public void updateDummyOrder(String id, HashMap<String, HashMap<String, List<Shop>>> maps1) {



            Criteria criteria =Criteria.where("id").is(id);
            Query query = new Query(criteria);

            Update update =Update.update("maps",maps1);

            mongoTemplate.updateFirst(query,update,DummyOrder.class,"t_dummyOrder");




    }

    @Override
    public  HashMap<String, List<Shop>>   getShopCarbyUserid(String userid) {
        Query query = new Query();
        if(userid!=null){
            Criteria regex = Criteria.where("userid").regex(userid);
            query.addCriteria(regex);
        }

        Order order = mongoTemplate.findOne(query, Order.class, "t_shop");
        HashMap<String, HashMap<String, List<Shop>>> maps = order.getMaps();
        HashMap<String, List<Shop>> stringListHashMap = maps.get(userid);

        return  stringListHashMap;
    }

    @Override
    public   HashMap<String, List<Shop>> getShopCarbyCookieid(String cookieid) {


            Query query = new Query();

            if(cookieid!=null){
                Criteria regex = Criteria.where("cookieid").regex(cookieid);
                query.addCriteria(regex);
            }

            DummyOrder dummyOrder = mongoTemplate.findOne(query, DummyOrder.class, "t_dummyOrder");
            HashMap<String, HashMap<String, List<Shop>>> maps = dummyOrder.getMaps();

            HashMap<String, List<Shop>> stringListHashMap = maps.get(cookieid);

        System.out.println("stringListHashMap = " + stringListHashMap + "");
        JSONObject jsonObject = new JSONObject();

        Set<String> strings = stringListHashMap.keySet();



        for (String string : strings) {

            List<Shop> shops = stringListHashMap.get(string);

            jsonObject.put("id",shops);

        }

        System.out.println("------------"+jsonObject);

        return   stringListHashMap;
        }



}
